import config from './constant.js'
var BLOCKSIZE = config.BLOCKSIZE;

export default class Block{
  constructor(x=0,y=0,width = BLOCKSIZE,height=BLOCKSIZE,imgSrc='../assets/rocket-enemy.png'){
    this.image = new Image();
    this.image.src = imgSrc;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  // 计算碰撞
  collision(other){
    if(
      Math.abs((this.x + this.width / 2) - (other.x + other.width / 2)) < Math.abs(this.width + other.width) / 2 -2 &&
      Math.abs((this.y + this.height / 2) - (other.y + other.height / 2)) < Math.abs(this.height + other.height) / 2 -2
      ){
        return true
    }
    return false;
  }

  render(context){
    context.drawImage(this.image,this.x,this.y,this.width,this.height);
    // var str = `(${this.x},${this.y})`;
    // context.fillStyle = '#fff'
    // context.fillText(str,this.x,this.y + 12)
  }
}